<?php 
namespace Wdl\helper;
 
/**
 *  $helper = new \Wdl\helper\MapSearch();
 *  $helper->keyword('购物');
 *  搜索小区周边的配套
 *  保存到对应的数据表记录
 */
class MapSearch{    
    public  $category = [
        '交通'=>"category=公交车站",
        '教育'=>"category=中学,小学,幼儿园",
        '医疗'=>"category=综合医院,急救中心,社区医院",
        '购物'=>"category=综合商场,超市",
        '休闲'=>"category=电影院,户外活动,游乐场",
    ];
    protected $key = '2ENBZ-2P5KN-KHCFW-S6WLJ-E7KXJ-MGB52';
    protected $sk = "1YpYc0z7sSPtvx6JaypAcVOtsbULqtoT";


    public function __construct($key, $sk){
        $this->key = $key;
        $this->sk = $sk;
    }
    public function keyword($keyword,$latitude = "25.260669",$longitude="110.180669"){
        load()->func('communication');

        
        $params =[
            //https://lbs.qq.com/dev/console/application/mine 
            //腾讯地图服务api控制台申请应用得到的key
            'key'=> $this->key , //
            'boundary'=> "nearby({$latitude},{$longitude},1000)",
            'page_size'=> 5,
            'page_index'=> 1,
            'keyword'=> $keyword, 
            'filter' => $this->category[$keyword] ,        
        ];
        //webserviceAPI接口采用签名方式，才能在服务器端获取接口数据
        $sk = $this->sk;

        $host = "https://apis.map.qq.com";
        $path = "/ws/place/v1/search";
        
        ksort($params);
        $querystr= $signstr = $dot = "";
        foreach($params as $key=> $val){
            $querystr.=$dot.$key."=" .urlencode($val);
            $signstr.=$dot.$key."=".$val;
            $dot = "&";
        }
        $sig  = md5($path."?". $signstr.$sk);

        $ret = file_get_contents($host.$path."?".$querystr ."&sig=".$sig);
        if($ret){
            $json = json_decode($ret, true);
            if($json && $json['status'] == 0){
                return $json['data'];
            }
        }
        return [];

        //$save_path = IA_ROOT. "/addons/kbwdl_shequzufang/vender/";
       // file_put_contents($save_path.'mapsearch.json', $ret);
    }
}